/****************************************************************************
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;
using namespace CocosDenshion;

Scene* HelloWorld::createScene()
{
    return HelloWorld::create();
}

// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{
    printf("Error while loading: %s\n", filename);
    printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Scene::init() )
    {
        return false;
    }

    // 获取屏幕的尺寸
    this->visibleSize = Director::getInstance()->getVisibleSize();
    this->origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    
    //设置游戏背景图片
    auto background=Sprite::create("mmbg.jpg");
    //设置背景图片坐标
    background->setPosition(this->visibleSize/2);
    //缩放背景图片
    background->setScale(0.5);
    this->addChild(background);
    //创建一个开始游戏的按钮
    auto btn1=MenuItemImage::create("btn1.png", "btn1act.png", CC_CALLBACK_1(HelloWorld::startGame,this));
    btn1->setPosition(this->visibleSize/2);
    btn1->setScale(0.4);
    //  创建动画1
    auto scaleone=ScaleTo::create(2, 0.45);
    //  创建动画2
    auto scaletwo=ScaleTo::create(2, 0.4);
    // 创建序列动画
    auto seq=Sequence::create(scaleone,scaletwo, NULL);
    // 创建循环动画
    auto rep=RepeatForever::create(seq);
    //按钮播放动画
    btn1->runAction(rep);

    auto btn2=MenuItemImage::create("set1.png","set1act.png",CC_CALLBACK_1(HelloWorld::setting,this));
    
    btn2->setPosition(Vec2(40,40));
    btn2->setScale(0.25);

    auto btn3=MenuItemImage::create("help.png","helpact.png",CC_CALLBACK_1(HelloWorld::about,this));
    
    btn3->setPosition(Vec2(40,110));
    btn3->setScale(0.35);

    auto btn4=MenuItemImage::create("e.png","eact.png",CC_CALLBACK_1(HelloWorld::exit,this));
    
    btn4->setPosition(Point(this->visibleSize.width-40,40));
    btn4->setScale(0.35);
    //创建菜单容器
    auto menu=Menu::create(btn1,btn2,btn3,btn4, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu);
    
    //播放背景音乐
    this->playBgMusic();
    
    ParticleSystem* particleSystem = ParticleRain::create();
    particleSystem->setTexture(TextureCache::sharedTextureCache()->addImage("fire.png"));
    particleSystem->setEmissionRate(2);
    this->addChild(particleSystem);
    return true;
}

// 开始游戏
void HelloWorld::startGame(Ref*pSender){
   // 跳转到游戏场景
    auto scene=GameScene::createScene(1);
    Director::getInstance()->replaceScene(scene);
}

// 游戏设置
void HelloWorld::setting(Ref*pSender){
    
    // 跳转到游戏设置界面
    auto scene=SettingLayer::createScene();
    Director::getInstance()->replaceScene(scene);
    
}
void HelloWorld::about(Ref*pSender){

    auto scene=AboutScene::createScene();
    Director::getInstance()->replaceScene(scene);
}

void HelloWorld::exit(Ref*pSender){
    
    //结束程序
    Director::getInstance()->end();
}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
    //Close the cocos2d-x game scene and quit the application
    Director::getInstance()->end();

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif

    /*To navigate back to native iOS screen(if present) without quitting the application  ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/

    //EventCustom customEndEvent("game_scene_close_event");
    //_eventDispatcher->dispatchEvent(&customEndEvent);
}
//播放背景音乐
void HelloWorld::playBgMusic(){

    //读取用户设置，记住用户的设置
    bool ison=UserDefault::getInstance()->getBoolForKey("bgmusic", true);
    
    if(ison){
     //如果设置打开就播放背景音乐
        SimpleAudioEngine::getInstance()->playBackgroundMusic("bgmusics.mp3");
    }
}



